<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="" prop="addtime">
      <el-input v-model="dataForm.addtime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="deletestatus">
      <el-input v-model="dataForm.deletestatus" placeholder=""></el-input>
    </el-form-item> -->
    <el-form-item label="分类名称" prop="floorName">
      <el-input v-model="dataForm.floorName" placeholder="分类名称"></el-input>
    </el-form-item>
    <el-form-item label="是否显示" prop="showStatus" v-if="dataForm.id != 0">
      <el-select v-model="dataForm.showStatus" placeholder="是否显示">
          <el-option
            v-for="item in dataForm.displayOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      <!-- <el-input v-model="dataForm.display" placeholder="是否显示"></el-input> -->
    </el-form-item>
    <el-form-item label="图标" prop="img">
      <el-upload
        :class="{hide:hideUpload}"
        :action="this.$http.adornUrl(`/business/class/uploadIcon?token=${this.$cookie.get('token')}`)"
        :limit="1"
        list-type="picture-card"
        :file-list="dataForm.img"
        :on-preview="handlePreview"
        :on-success="handleSuccess"
        :on-remove="handleRemove"
        :on-exceed="onExceed">
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible" :append-to-body='true'>
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
    </el-form-item>
    <el-form-item label="显示顺序" prop="seq">
      <el-input type="number" style="width: 120px" v-model="dataForm.seq" placeholder="显示顺序"></el-input>
    </el-form-item>
    <el-form-item label="跳转类型" prop="jumpType">
      <el-row  style="display: flex;">
        <el-select v-model="dataForm.jumpType" placeholder="分类" value="" style="margin-right: 30px">
          <el-option label="无跳转" :value="6"></el-option>
          <el-option label="商品分类" :value="0"></el-option>
          <el-option label="联盟店分类" :value="1"></el-option>
          <el-option label="外链" :value="3"></el-option>
          <!-- <el-option label="福利商城" :value="9"></el-option>
          <el-option label="兑换商城" :value="10"></el-option> -->
          <el-option label="商学院" :value="11"></el-option>
          <el-option label="特殊处理" :value="12"></el-option>
          <el-option label="分享" :value="13"></el-option>
          <el-option label="分类导航页" :value="14"></el-option>
          <el-option label="商城一级分类" :value="17"></el-option>
          <el-option label="签到" :value="19"></el-option>
        </el-select>
        <el-input v-model="dataForm.url" placeholder="请输入跳转类型链接"></el-input>
      </el-row>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input type="textarea" v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        hideUpload: false,
        limitCount: 1,
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        dataForm: {
          id: 0,
          bannerType: '',
          jumpType: '',
          floorName: '',
          url: '',
          urlData: '',
          remark: '',
          showStatus: '',
          seq: '',
          img: [],
          displayOptions: [{
            value: 1,
            label: '显示'
          }, {
            value: 0,
            label: '不显示'
          }]
        },
        dataRule: {
          floorName: [
            { required: true, message: '分类名称不能为空', trigger: 'blur' }
          ],
          showStatus: [
            { required: true, message: '显示类型不能为空', trigger: 'blur' }
          ],
          seq: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 图片预览
      handlePreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      // 文件上传成功的钩子函数
      handleSuccess (res, file, fileList) {
        this.hideUpload = fileList.length >= this.limitCount
        this.$message({
          type: 'info',
          message: '图片上传成功',
          duration: 6000
        })
        if (res.code === 0) {
          this.dataForm.img.push({name: 'tupian', url: res.data[0].urlList[0]})
        }
      },
      // 删除文件之前的钩子函数
      handleRemove (file, fileList) {
        this.hideUpload = fileList.length >= this.limitCount
        for (var i = 0; i < this.dataForm.img.length; i++) {
          var picc = this.dataForm.img[i]
          if (picc.url === file.url) {
            this.dataForm.img.splice(i, 1)
          }
        }
        this.$message({
          type: 'info',
          message: '已删除原有图片',
          duration: 6000
        })
      },
      // 上传的文件个数超出设定时触发的函数
      onExceed (files, fileList) {
        this.$message({
          type: 'info',
          message: '最多只能上传1个图片',
          duration: 6000
        })
      },
      init (id) {
        this.hideUpload = false
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/class/getIconById/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.floorName = data.data.floorName
                this.dataForm.showStatus = data.data.showStatus
                this.dataForm.img.push({name: 'tupian', url: 'http://image.yilianbaihui.cn/consumer/' + data.data.imgPath + '/' + data.data.imgName})
                this.dataForm.seq = data.data.seq
                this.dataForm.bannerType = data.data.bannerType
                this.dataForm.jumpType = data.data.jumpType
                this.dataForm.url = data.data.url
                this.dataForm.urlData = data.data.urlData
                this.dataForm.remark = data.data.remark
              }
              this.hideUpload = true
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        var picArr = []
        if (this.dataForm.img.length >= 1) {
          picArr.push(this.dataForm.img[0].url)
        } else {
          alert('请上传图标')
          return false
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/banner/${!this.dataForm.id ? 'addIconDetails' : 'modifyIconDetails'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'floorName': this.dataForm.floorName,
                'imgName': this.dataForm.img[0].url.substring(this.dataForm.img[0].url.lastIndexOf('/') + 1, this.dataForm.img[0].url.length),
                'showStatus': this.dataForm.showStatus,
                'seq': this.dataForm.seq,
                'bannerType': this.dataForm.bannerType,
                'jumpType': this.dataForm.jumpType,
                'url': this.dataForm.url,
                'urlData': this.dataForm.urlDate,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
  .hide .el-upload--picture-card {  display: none; }
</style>
